package org.gdstash.ui;

import org.gdstash.character.GDChar;
import org.gdstash.db.DBStashItem;
import org.gdstash.db.DBStat;
import org.gdstash.db.SelectionCriteria;
import org.gdstash.db.criteria.CriteriaCombination;
import org.gdstash.formula.GDFormulaList;
import org.gdstash.item.GDItem;
import org.gdstash.item.GDStash;
import org.gdstash.item.GDTransmute;
import org.gdstash.trade.GDTradeList;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class GDMassImportPane extends AdjustablePanel implements GDUISearch {
    private String strSave;
    private GDStashFrame frame;
    private JLabel lblDir;
    private JButton btnHelp;
    private JButton btnSetDir;
    private JButton btnLoadDir;
    private JButton btnLoadGDS;
    private JButton btnLoadIAS;
    private JButton btnImport;
    private JButton btnSearch;
    private JButton btnGDSExp;
    private JButton btnAddTransmute;
    private JButton btnFullTransmute;
    private JButton btnFullBlueprint;
    private JButton btnUniqueClipboard;
    private JButton btnLegendaryClipboard;
    private String strDir;
    private File fileDir;
    private List<GDItem> items;
    private GDItemImportTablePane pnlTable;
    private JPanel pnlDir;
    private JPanel pnlStash;
    private JPanel pnlDB;
    private JPanel pnlTransmute;
    private JPanel pnlBlueprint;
    private JPanel pnlClipboard;
    private GDTabbedSearchDialog dlgSearch;

    public GDMassImportPane(final GDStashFrame frame) {
        this.strSave = GDStashFrame.iniConfig.sectDir.savePath;
        this.frame = frame;
        this.adjustUI();
        final BorderLayout layout = new BorderLayout();
        final JPanel pnlTable = this.buildTablePanel();
        final JPanel pnlSide = this.buildSideButtonPanel();
        this.setLayout(layout);
        this.add(pnlSide, "West");
        this.add(pnlTable, "Center");
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.btnSetDir == null) {
            (this.btnSetDir = new JButton()).addActionListener(new ImportDirListener());
        }
        this.btnSetDir.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_SET_IMPORT_DIR"));
        this.btnSetDir.setIcon(GDImagePool.iconBtnDir24);
        this.btnSetDir.setFont(fntButton);
        if (this.lblDir == null) {
            this.lblDir = new JLabel();
        }
        this.lblDir.setFont(fntLabel);
        if (this.btnHelp == null) {
            (this.btnHelp = new JButton()).addActionListener(new GDHelpActionListener("02_im_export.html"));
        }
        this.btnHelp.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_HELP"));
        this.btnHelp.setIcon(GDImagePool.iconBtnQuestion24);
        this.btnHelp.setFont(fntButton);
        GDStashFrame.setMnemonic(this.btnHelp, "MNC_HELP");
        if (this.btnLoadDir == null) {
            (this.btnLoadDir = new JButton()).addActionListener(new FileLoadListener());
        }
        this.btnLoadDir.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_LOAD_TRANSFER"));
        this.btnLoadDir.setIcon(GDImagePool.iconMassItemLoad24);
        this.btnLoadDir.setFont(fntButton);
        if (this.btnLoadGDS == null) {
            (this.btnLoadGDS = new JButton()).addActionListener(new GDStashLoadListener());
        }
        this.btnLoadGDS.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_LOAD_GDS_FILE"));
        this.btnLoadGDS.setIcon(GDImagePool.iconMassGDSLoad24);
        this.btnLoadGDS.setFont(fntButton);
        if (this.btnLoadIAS == null) {
            (this.btnLoadIAS = new JButton()).addActionListener(new ItemAssistantLoadListener());
        }
        this.btnLoadIAS.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_LOAD_IAS_FILE"));
        this.btnLoadIAS.setIcon(GDImagePool.iconMassIASLoad24);
        this.btnLoadIAS.setFont(fntButton);
        if (this.btnImport == null) {
            (this.btnImport = new JButton()).addActionListener(new ImportDBListener());
        }
        this.btnImport.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_IMPORT_ITEMS"));
        this.btnImport.setIcon(GDImagePool.iconMassItemImport24);
        this.btnImport.setFont(fntButton);
        if (this.btnSearch == null) {
            (this.btnSearch = new JButton()).addActionListener(new SearchItemsListener());
        }
        this.btnSearch.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_SEARCH"));
        this.btnSearch.setIcon(GDImagePool.iconBtnSearch24);
        this.btnSearch.setFont(fntButton);
        if (this.btnGDSExp == null) {
            (this.btnGDSExp = new JButton()).addActionListener(new GDStashExportListener());
        }
        this.btnGDSExp.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_EXPORT_GDS_FILE"));
        this.btnGDSExp.setIcon(GDImagePool.iconMassGDSExport24);
        this.btnGDSExp.setFont(fntButton);
        if (this.btnAddTransmute == null) {
            (this.btnAddTransmute = new JButton()).addActionListener(new AddTransmuteListener());
        }
        this.btnAddTransmute.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_TRANSMUTE_ADD"));
        this.btnAddTransmute.setIcon(GDImagePool.iconConfigTransmuteAdd24);
        this.btnAddTransmute.setFont(fntButton);
        if (this.btnFullTransmute == null) {
            (this.btnFullTransmute = new JButton()).addActionListener(new FullTransmuteListener());
        }
        this.btnFullTransmute.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_TRANSMUTE_FULL"));
        this.btnFullTransmute.setIcon(GDImagePool.iconConfigTransmuteFull24);
        this.btnFullTransmute.setFont(fntButton);
        if (this.btnFullBlueprint == null) {
            (this.btnFullBlueprint = new JButton()).addActionListener(new FullBlueprintListener());
        }
        this.btnFullBlueprint.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_BLUEPRINT_FULL"));
        this.btnFullBlueprint.setIcon(GDImagePool.iconMassBlueprints24);
        this.btnFullBlueprint.setFont(fntButton);
        if (this.btnUniqueClipboard == null) {
            (this.btnUniqueClipboard = new JButton()).addActionListener(new UniqueClipboardListener());
        }
        this.btnUniqueClipboard.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_UNQIUES_CLIPBOARD"));
        this.btnUniqueClipboard.setIcon(GDImagePool.iconMassUniqueClipboard24);
        this.btnUniqueClipboard.setFont(fntButton);
        if (this.btnLegendaryClipboard == null) {
            (this.btnLegendaryClipboard = new JButton()).addActionListener(new LegendaryClipboardListener());
        }
        this.btnLegendaryClipboard.setText(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_LEGEND_CLIPBOARD"));
        this.btnLegendaryClipboard.setIcon(GDImagePool.iconMassLegendClipboard24);
        this.btnLegendaryClipboard.setFont(fntButton);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        TitledBorder text = null;
        if (this.pnlDir != null) {
            text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BRD_DIRECTORY"));
            text.setTitleFont(fntBorder);
            this.pnlDir.setBorder(text);
        }
        if (this.pnlStash != null) {
            text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BRD_IMPORT_ITEMS"));
            text.setTitleFont(fntBorder);
            this.pnlStash.setBorder(text);
        }
        if (this.pnlDB != null) {
            text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BRD_EXPORT_DB"));
            text.setTitleFont(fntBorder);
            this.pnlDB.setBorder(text);
        }
        if (this.pnlTable == null) {
            this.pnlTable = new GDItemImportTablePane((List<GDItem>) null);
        }
        this.pnlTable.adjustUI();
    }

    private JPanel buildTablePanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel pnlImpExp = this.buildImpExpPanel();
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlImpExp).addComponent(this.pnlTable));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlImpExp)).addGroup(layout.createParallelGroup().addComponent(this.pnlTable));
        layout.setVerticalGroup(vGroup);
        panel.setBorder(compound);
        return panel;
    }

    private JPanel buildImpExpPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.pnlDir = this.buildDirPanel();
        final JPanel pnlButton = this.buildImExButtonPanel();
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDir).addComponent(pnlButton));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlDir)).addGroup(layout.createParallelGroup().addComponent(pnlButton));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlDir, pnlButton);
        panel.setBorder(compound);
        return panel;
    }

    private JPanel buildImExButtonPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.btnLoadDir)).addGroup(layout.createParallelGroup().addComponent(this.btnLoadGDS)).addGroup(layout.createParallelGroup().addComponent(this.btnLoadIAS)).addGroup(layout.createParallelGroup().addComponent(this.btnImport)).addGroup(layout.createParallelGroup().addComponent(this.btnSearch)).addGroup(layout.createParallelGroup().addComponent(this.btnGDSExp));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.btnLoadDir).addComponent(this.btnLoadGDS).addComponent(this.btnLoadIAS).addComponent(this.btnImport).addComponent(this.btnSearch).addComponent(this.btnGDSExp));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.btnLoadDir, this.btnLoadGDS);
        layout.linkSize(0, this.btnLoadDir, this.btnLoadIAS);
        layout.linkSize(0, this.btnLoadDir, this.btnImport);
        layout.linkSize(0, this.btnLoadDir, this.btnSearch);
        layout.linkSize(0, this.btnLoadDir, this.btnGDSExp);
        layout.linkSize(1, this.btnLoadDir, this.btnLoadGDS);
        layout.linkSize(1, this.btnLoadDir, this.btnLoadIAS);
        layout.linkSize(1, this.btnLoadDir, this.btnImport);
        layout.linkSize(1, this.btnLoadDir, this.btnSearch);
        layout.linkSize(1, this.btnLoadDir, this.btnGDSExp);
        panel.setBorder(compound);
        return panel;
    }

    private JPanel buildSideButtonPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final JPanel panel = new JPanel();
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.btnUniqueClipboard).addComponent(this.btnLegendaryClipboard).addComponent(this.btnFullBlueprint).addComponent(this.btnAddTransmute).addComponent(this.btnFullTransmute));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.btnUniqueClipboard)).addGroup(layout.createParallelGroup().addComponent(this.btnLegendaryClipboard)).addGroup(layout.createParallelGroup().addComponent(this.btnFullBlueprint)).addGroup(layout.createParallelGroup().addComponent(this.btnAddTransmute)).addGroup(layout.createParallelGroup().addComponent(this.btnFullTransmute));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.btnUniqueClipboard, this.btnLegendaryClipboard);
        layout.linkSize(0, this.btnUniqueClipboard, this.btnFullBlueprint);
        layout.linkSize(0, this.btnUniqueClipboard, this.btnAddTransmute);
        layout.linkSize(0, this.btnUniqueClipboard, this.btnFullTransmute);
        layout.linkSize(1, this.btnUniqueClipboard, this.btnLegendaryClipboard);
        layout.linkSize(1, this.btnUniqueClipboard, this.btnFullBlueprint);
        layout.linkSize(1, this.btnUniqueClipboard, this.btnAddTransmute);
        layout.linkSize(1, this.btnUniqueClipboard, this.btnFullTransmute);
        panel.setBorder(compound);
        return panel;
    }

    private JPanel buildDirPanel() {
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final Border text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BRD_DIRECTORY"));
        final JPanel panel = new JPanel();
        final JPanel pnlHelp = this.buildHelpButtonPanel();
        this.strDir = GDStashFrame.iniConfig.sectDir.savePath;
        if (this.strDir != null) {
            this.fileDir = new File(this.strDir);
        }
        if (this.strDir == null) {
            this.lblDir.setText("");
        } else {
            this.lblDir.setText(this.strDir);
        }
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlHelp)).addGroup(layout.createParallelGroup().addComponent(this.lblDir));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlHelp).addComponent(this.lblDir));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, pnlHelp, this.lblDir);
        panel.setBorder(text);
        return panel;
    }

    private JPanel buildHelpButtonPanel() {
        final BorderLayout layout = new BorderLayout();
        final JPanel panel = new JPanel();
        panel.setLayout(layout);
        panel.add(this.btnSetDir, "Center");
        panel.add(this.btnHelp, "West");
        int size = 12;
        if (GDStashFrame.iniConfig != null) {
            size = GDStashFrame.iniConfig.sectUI.fontSize;
        }
        final Dimension dimPref = new Dimension(20 * size, 2 * size);
        final Dimension dimMax = new Dimension(35 * size, 2 * size);
        panel.setPreferredSize(dimPref);
        panel.setMaximumSize(dimMax);
        return panel;
    }

    private void processDir(final File dir) {
        if (dir == null) {
            return;
        }
        final File[] files = dir.listFiles();
        if (files == null) {
            return;
        }
        for (int i = 0; i < files.length; ++i) {
            final File file = files[i];
            if (file != null) {
                if (file.isDirectory()) {
                    this.processDir(file);
                }
                if (file.isFile()) {
                    this.processFile(file);
                }
            }
        }
    }

    private void processFile(final File file) {
        final String s = file.getName().toUpperCase(GDConstants.LOCALE_US);
        if (s.endsWith(".GST") || s.endsWith(".GSH")) {
            if (s.equals("TRANSMUTES.GST") || s.equals("TRANSMUTES.GSH")) {
                return;
            }
            if (s.equals("FORMULAS.GST") || s.equals("FORMULAS.GSH")) {
                final GDFormulaList fl = new GDFormulaList(file);
                fl.read();
                if (fl.hasErrors()) {
                    return;
                }
                this.items.addAll(fl.getItems());
            } else {
                final GDStash stash = new GDStash(file);
                if (stash.hasStashErrors()) {
                    return;
                }
                this.items.addAll(stash.getItems());
            }
        } else {
            if (!s.equals("PLAYER.GDC")) {
                return;
            }
            final GDChar gdc = new GDChar(file);
            gdc.read();
            if (gdc.hasErrors()) {
                return;
            }
            this.items.addAll(gdc.getItems());
        }
    }

    private void loadStash(final File file) {
        final String s = file.getName();
        int pos = s.indexOf(".GST");
        if (pos == -1) {
            pos = s.indexOf(".gst");
        }
        if (pos == -1) {
            pos = s.indexOf(".GSH");
        }
        if (pos == -1) {
            pos = s.indexOf(".gsh");
        }
        if (pos == s.length() - 4) {
            final GDStash stash = new GDStash(file);
            if (stash.hasStashErrors()) {
                return;
            }
            this.items.addAll(stash.getItems());
        }
    }

    @Override
    public void search(final SelectionCriteria criteria) {
        criteria.noEnemyOnly = true;
        List<GDItem> itemsAll = DBStashItem.getGDItemByCriteria(criteria, CriteriaCombination.Soulbound.INCLUDED, CriteriaCombination.SC_HC.ALL, false, null);
        if (criteria.combiMode == SelectionCriteria.CombinationMode.AND) {
            final List<GDItem> list = new LinkedList<GDItem>();
            for (final GDItem gdi : itemsAll) {
                if (!DBStat.statsMeetCriteria(gdi.getStatList(), criteria)) {
                    continue;
                }
                if (criteria.dmgConversionFrom != null && !gdi.hasConvertIn(criteria.dmgConversionFrom)) {
                    continue;
                }
                if (criteria.dmgConversionTo != null && !gdi.hasConvertOut(criteria.dmgConversionTo)) {
                    continue;
                }
                if (criteria.petBonus && !gdi.hasPetBonus()) {
                    continue;
                }
                list.add(gdi);
            }
            itemsAll = list;
        }
        this.items = itemsAll;
        this.pnlTable.setItems(this.items);
        GDMsgLogger.showLog(this, GDMsgFormatter.getString(GDMsgFormatter.rbMsg, "ERR_SEARCH"));
    }

    private class ImportDirListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final JFileChooser chooser = new JFileChooser(GDMassImportPane.this.fileDir);
            chooser.setFileSelectionMode(1);
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showOpenDialog(GDMassImportPane.this) == 0) {
                try {
                    GDMassImportPane.this.fileDir = chooser.getSelectedFile();
                    GDMassImportPane.this.strDir = GDMassImportPane.this.fileDir.getCanonicalPath();
                    GDMassImportPane.this.lblDir.setText(GDMassImportPane.this.strDir);
                } catch (IOException ex) {
                }
            }
        }
    }

    private class FileLoadListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDMassImportPane.this.strDir == null) {
                return;
            }
            GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
            GDMassImportPane.this.items = (List<GDItem>) new LinkedList();
            final File file = new File(GDMassImportPane.this.strDir);
            if (file.isDirectory()) {
                GDMassImportPane.this.processDir(file);
            }
            if (file.isFile()) {
                GDMassImportPane.this.processFile(file);
            }
            GDMassImportPane.this.pnlTable.setItems(GDMassImportPane.this.items);
            GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
            GDMsgLogger.showLog(GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_IMPORT"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_IMPORT"));
        }
    }

    private class GDStashLoadListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final JFileChooser chooser = new JFileChooser(GDMassImportPane.this.strSave);
            chooser.setFileSelectionMode(0);
            chooser.setFileFilter(new FileNameExtensionFilter(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_GD_STASH"), new String[]{"gds"}));
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showOpenDialog(GDMassImportPane.this) == 0) {
                final File fStash = chooser.getSelectedFile();
                if (!fStash.exists()) {
                    final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_FILE_NOT_EXIST"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                    dialog.setVisible(true);
                    return;
                }
                GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                GDMassImportPane.this.items = DBStashItem.loadGDS(fStash, GDConstants.CHARSET_STASH);
                GDMassImportPane.this.pnlTable.setItems(GDMassImportPane.this.items);
                GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                GDMsgLogger.showLog(GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_IMPORT"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_IMPORT"));
            }
        }
    }

    private class ItemAssistantLoadListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final JFileChooser chooser = new JFileChooser(GDMassImportPane.this.strSave);
            chooser.setFileSelectionMode(0);
            chooser.setFileFilter(new FileNameExtensionFilter(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_ITEM_ASSISTANT"), new String[]{"ias"}));
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showOpenDialog(GDMassImportPane.this) == 0) {
                final File fStash = chooser.getSelectedFile();
                if (!fStash.exists()) {
                    final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_FILE_NOT_EXIST"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                    dialog.setVisible(true);
                    return;
                }
                GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                GDMassImportPane.this.items = DBStashItem.loadIAS(fStash);
                GDMassImportPane.this.pnlTable.setItems(GDMassImportPane.this.items);
                GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                GDMsgLogger.showLog(GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_IMPORT"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_IMPORT"));
            }
        }
    }

    private class ImportDBListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final List<GDItem> items = GDMassImportPane.this.pnlTable.getSelectedItems();
            if (items == null || items.isEmpty()) {
                final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_ITEM_NO_SEL"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog.setVisible(true);
                return;
            }
            GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
            DBStashItem.storeGDItems(items);
            GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
            GDMsgLogger.showLog(GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_STORE"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_STORE"), true);
        }
    }

    private class SearchItemsListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDMassImportPane.this.dlgSearch == null) {
                GDMassImportPane.this.dlgSearch = new GDTabbedSearchDialog(GDMassImportPane.this.frame, GDMassImportPane.this, GDTabbedSearchDialog.Mode.SEARCH);
            }
            GDMassImportPane.this.dlgSearch.setVisible(true);
        }
    }

    private class GDStashExportListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            final JFileChooser chooser = new JFileChooser(GDMassImportPane.this.strSave);
            chooser.setFileSelectionMode(0);
            chooser.setFileFilter(new FileNameExtensionFilter(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_GD_STASH"), new String[]{"gds"}));
            chooser.setAcceptAllFileFilterUsed(false);
            if (chooser.showSaveDialog(GDMassImportPane.this) == 0) {
                try {
                    String filename = chooser.getSelectedFile().getCanonicalPath();
                    if (!filename.endsWith(".gds")) {
                        filename += ".gds";
                    }
                    final File fStash = new File(filename);
                    final List<GDItem> items = GDMassImportPane.this.pnlTable.getSelectedItems();
                    if (items != null && !items.isEmpty()) {
                        DBStashItem.writeGDStashItemListGDS(fStash, GDConstants.CHARSET_STASH, items);
                    } else {
                        DBStashItem.writeAllGDS(fStash, GDConstants.CHARSET_STASH);
                    }
                } catch (IOException ex) {
                    GDMsgLogger.addError(ex);
                }
                GDMsgLogger.showLog(GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_EXPORT"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_EXPORT"), true);
            }
        }
    }

    private class AddTransmuteListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDMassImportPane.this.strSave == null) {
                return;
            }
            if (GDMassImportPane.this.strSave.isEmpty()) {
                return;
            }
            if (GDStashFrame.dbConfig.gddbInit) {
                String dirname = GDMassImportPane.this.strSave;
                final String mod = GDMassImportPane.this.frame.getSelectedMod();
                if (mod != null && !mod.isEmpty()) {
                    dirname = dirname + GDConstants.FILE_SEPARATOR + mod;
                }
                boolean scExists = false;
                boolean hcExists = false;
                final String scFilename = dirname + GDConstants.FILE_SEPARATOR + "transmutes.gst";
                File file = new File(scFilename);
                if (file.exists()) {
                    scExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDTransmute gdt = new GDTransmute(file);
                        gdt.read(null);
                        if (!gdt.hasErrors()) {
                            gdt.insertCollectedItems();
                            gdt.write();
                        }
                    } catch (Exception ex) {
                        GDMsgLogger.addError(ex);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                final String hcFilename = dirname + GDConstants.FILE_SEPARATOR + "transmutes.gsh";
                file = new File(hcFilename);
                if (file.exists()) {
                    hcExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDTransmute gdt2 = new GDTransmute(file);
                        gdt2.read(null);
                        if (!gdt2.hasErrors()) {
                            gdt2.insertCollectedItems();
                            gdt2.write();
                        }
                    } catch (Exception ex2) {
                        GDMsgLogger.addError(ex2);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                if (!scExists && !hcExists) {
                    final Object[] args = {scFilename};
                    final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg);
                    final Object[] args2 = {hcFilename};
                    final String msg2 = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg2);
                }
                GDMsgLogger.showLog(GDMassImportPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_TRANSMUTE_ADD"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_TRANSMUTE_ADD"), true, false);
            } else {
                final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_DB_NOT_IMPORTED"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog.setVisible(true);
            }
        }
    }

    private class FullTransmuteListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDMassImportPane.this.strSave == null) {
                return;
            }
            if (GDMassImportPane.this.strSave.isEmpty()) {
                return;
            }
            if (GDStashFrame.dbConfig.gddbInit) {
                String dirname = GDMassImportPane.this.strSave;
                final String mod = GDMassImportPane.this.frame.getSelectedMod();
                if (mod != null && !mod.isEmpty()) {
                    dirname = dirname + GDConstants.FILE_SEPARATOR + mod;
                }
                boolean scExists = false;
                boolean hcExists = false;
                final String scFilename = dirname + GDConstants.FILE_SEPARATOR + "transmutes.gst";
                File file = new File(scFilename);
                if (file.exists()) {
                    scExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDTransmute gdt = new GDTransmute(file);
                        gdt.read(null);
                        if (!gdt.hasErrors()) {
                            gdt.insertAllItems();
                            gdt.write();
                        }
                    } catch (Exception ex) {
                        GDMsgLogger.addError(ex);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                final String hcFilename = dirname + GDConstants.FILE_SEPARATOR + "transmutes.gsh";
                file = new File(hcFilename);
                if (file.exists()) {
                    hcExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDTransmute gdt2 = new GDTransmute(file);
                        gdt2.read(null);
                        if (!gdt2.hasErrors()) {
                            gdt2.insertAllItems();
                            gdt2.write();
                        }
                    } catch (Exception ex2) {
                        GDMsgLogger.addError(ex2);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                if (!scExists && !hcExists) {
                    final Object[] args = {scFilename};
                    final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg);
                    final Object[] args2 = {hcFilename};
                    final String msg2 = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg2);
                }
                GDMsgLogger.showLog(GDMassImportPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_TRANSMUTE_FULL"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_TRANSMUTE_FULL"), true, false);
            } else {
                final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_DB_NOT_IMPORTED"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog.setVisible(true);
            }
        }
    }

    private class UniqueClipboardListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDStashFrame.dbConfig.gddbInit) {
                GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                final List<GDItem> items = GDMassImportPane.this.pnlTable.getSelectedItems();
                if (items != null && !items.isEmpty()) {
                    GDTradeList.extractUniques(items);
                } else {
                    GDTradeList.extractUniques();
                }
                GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                final GDLogoDialog dialog = new GDLogoDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_UNIQUES_TO_CLIPBOARD"), 2, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFORMATION"), true);
                dialog.setVisible(true);
            } else {
                final GDDialog dialog2 = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_DB_NOT_IMPORTED"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog2.setVisible(true);
            }
        }
    }

    private class LegendaryClipboardListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDStashFrame.dbConfig.gddbInit) {
                GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                final List<GDItem> items = GDMassImportPane.this.pnlTable.getSelectedItems();
                if (items != null && !items.isEmpty()) {
                    GDTradeList.extractLegendaries(items);
                } else {
                    GDTradeList.extractLegendaries();
                }
                GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                final GDLogoDialog dialog = new GDLogoDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "TXT_LEGEND_TO_CLIPBOARD"), 2, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "INFORMATION"), true);
                dialog.setVisible(true);
            } else {
                final GDDialog dialog2 = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_DB_NOT_IMPORTED"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog2.setVisible(true);
            }
        }
    }

    private class FullBlueprintListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            if (GDMassImportPane.this.strSave == null) {
                return;
            }
            if (GDMassImportPane.this.strSave.isEmpty()) {
                return;
            }
            if (GDStashFrame.dbConfig.gddbInit) {
                String dirname = GDMassImportPane.this.strSave;
                final String mod = GDMassImportPane.this.frame.getSelectedMod();
                if (mod != null && !mod.isEmpty()) {
                    dirname = dirname + GDConstants.FILE_SEPARATOR + mod;
                }
                boolean scExists = false;
                boolean hcExists = false;
                final String scFilename = dirname + GDConstants.FILE_SEPARATOR + "formulas.gst";
                File file = new File(scFilename);
                if (file.exists()) {
                    scExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDFormulaList fl = new GDFormulaList(file);
                        fl.read();
                        if (!fl.hasErrors()) {
                            fl.addBlueprints();
                            fl.write();
                        }
                    } catch (Exception ex) {
                        GDMsgLogger.addError(ex);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                final String hcFilename = dirname + GDConstants.FILE_SEPARATOR + "formulas.gsh";
                file = new File(hcFilename);
                if (file.exists()) {
                    hcExists = true;
                    GDMassImportPane.this.setCursor(Cursor.getPredefinedCursor(3));
                    try {
                        final GDFormulaList fl2 = new GDFormulaList(file);
                        fl2.read();
                        if (!fl2.hasErrors()) {
                            fl2.addBlueprints();
                            fl2.write();
                        }
                    } catch (Exception ex2) {
                        GDMsgLogger.addError(ex2);
                    }
                    GDMassImportPane.this.setCursor(Cursor.getDefaultCursor());
                }
                if (!scExists && !hcExists) {
                    final Object[] args = {scFilename};
                    final String msg = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg);
                    final Object[] args2 = {hcFilename};
                    final String msg2 = GDMsgFormatter.format(GDMsgFormatter.rbMsg, "ERR_FILE_NOT_FOUND", args);
                    GDMsgLogger.addError(msg2);
                }
                GDMsgLogger.showLog(GDMassImportPane.this, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "SUCC_BLUEPRINT_FULL"), GDLog.MessageType.Success, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_BLUEPRINT_FULL"), true, false);
            } else {
                final GDDialog dialog = new GDDialog(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERR_DB_NOT_IMPORTED"), 4, GDMassImportPane.this.frame, GDMsgFormatter.getString(GDMsgFormatter.rbUI, "ERROR"), true);
                dialog.setVisible(true);
            }
        }
    }
}
